<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Menu_model short summary.
 *
 * Menu_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Menu_model extends CI_Model
{
    public $menuid;
    public $parentid;
    public $weight;
    public $title;
    public $arrparentid;
    public $child;
    public $arrchild;
    public $path;
    public $entitle;
    public $depth;
    public $menu_control;
    public $menu_action;
    public $icon   ;
    public $isuse;
    public $page_type;
    public $isdrop;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function get_top_level_menu()
    {
        $sql="select * from menu where isuse=1 and parentid=0 and depth=1 order by weight desc,menuid asc";
        return $this->db->query($sql)->result();
    }

    public function get_all_top_menu()
    {
        $sql="select * from menu where isuse=1 and parentid=0 and depth=1 order by weight desc,menuid asc";
        return $this->db->query($sql)->result();
    }

    public function get_top_menu($menuid)
    {
        $sql="select * from menu where menuid=$menuid";
        return $this->db->query($sql)->row();
    }

    public function get_all_child_menu_by_id($parentid)
    {
        $sql="select * from menu where parentid=$parentid order by weight desc";
        return $this->db->query($sql)->result();
    }

    //导航栏目添加
    public function insert_top_menu($menuModel)
    {
        $this->db->trans_begin();
        $this->db->insert('menu',$menuModel);
        $insertId=$this->db->insert_id();//获取添加数据后的主键Id
        $update_arrchild_sql="update menu set arrchild='$insertId,',path='$insertId,' where menuid=$insertId";
        $this->db->query($update_arrchild_sql);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }
    //导航栏目修改
    public function edit_top_menu($menuModel,$relation)//
    {
        $this->db->trans_begin();
        $sql="update menu set title='$menuModel->title',entitle='$menuModel->entitle',menu_control='$menuModel->menu_control',menu_action='$menuModel->menu_action',page_type=$menuModel->page_type where menuid=$menuModel->menuid";
        $this->db->query($sql);
        if(!empty($relation) && $relation>0)
        {
            $sql_relation_child="update menu set menu_control='$menuModel->menu_control',menu_action='$menuModel->menu_action',page_type=$menuModel->page_type where path like '$menuModel->menuid,%' and arrparentid like '0,$menuModel->menuid%' and menuid<>$menuModel->menuid";
            $this->db->query($sql_relation_child);
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }
    //更新栏目权重
    public function set_menu_weight($menuModel)
    {
        $sql="update menu set weight=$menuModel->weight where menuid=$menuModel->menuid";
        return $this->db->query($sql);
    }

    public function edit_top_menu_drop($menuModel)
    {
        $sql="update menu set isdrop=$menuModel->isdrop where menuid=$menuModel->menuid";
        return $this->db->query($sql);
    }
    //删除导航栏目
    public function del_top_menu($menuid)
    {
        $this->db->trans_begin();
        $sql="delete  from menu where menuid=$menuid;";
        //echo $sql;die;
        $this->db->query($sql);
        $sql_del_child="delete  from menu where path like '$menuid,%' and arrparentid like '0,$menuid%';";
        $this->db->query($sql_del_child);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    //添加导航栏目验证控制器是否唯一
    public function check_add_top_menu_controller($val)
    {
        $sql="select count(*) as menu_count from menu where parentid=0 and depth=1 and menu_control='".$val."'";
        return $this->db->query($sql)->row();
    }
    //修改导航栏目验证控制器是否唯一
    public function check_eidt_top_menu_controller($val,$mark)
    {
        $sql="select count(*) as menu_count from menu where parentid=0 and depth=1 and menu_control='".$val."' and menuid<>$mark";
        return $this->db->query($sql)->row();
    }

    //子集添加验证
    public function check_add_child_menu_controller($val,$parentid)
    {
        $sql_parent_menu="select * from menu where menuid=$parentid";
        $menu_model=$this->db->query($sql_parent_menu)->row();
        $sql="select count(*) as menu_count from menu where path not like '$menu_model->menuid,%' and arrparentid not like '0,$menu_model->menuid,%' and menu_control='".$val."'";
        //echo($sql);
        return $this->db->query($sql)->row();
    }
    //子集修改验证
    public function check_edit_child_menu_controller($val,$mark,$parentid)
    {
        $sql_parent_menu="select * from menu where menuid=$parentid";
        $menu_model=$this->db->query($sql_parent_menu)->row();
        $sql="select count(*) as menu_count from menu where path not like '$menu_model->menuid,%' and arrparentid not like '0,$menu_model->menuid%' and menu_control='".$val."' and  menuid<>$mark";
        //echo($sql);
        return $this->db->query($sql)->row();
    }

    //子集操作

    //由父级id查询子集集合
    public function get_childs_by_parentid($parentid)
    {
        $sql="select * from menu where parentid=$parentid order by weight desc,menuid asc";
        return $this->db->query($sql)->result();
    }

    public function get_menu_by_id($menuid)
    {
        $sql="select * from menu where menuid=$menuid";
        return $this->db->query($sql)->row();
    }

    //删除子集栏目
    public function del_child_menu($menuid)
    {
        $this->db->trans_begin();
        $current_menu=$this->get_menu_by_id($menuid);
        $sql="delete  from menu where menuid=$menuid;";
        //echo $sql;die;
        $this->db->query($sql);
        $sql_del_child="delete  from menu where path like '%$menuid,%' and arrparentid like '0,%$menuid%';";
        $this->db->query($sql_del_child);

        //更新父级
        $parent_menu=$this->get_menu_by_id($current_menu->parentid);
        $parent_menu->arrchild=str_replace($menuid.',','',$parent_menu->arrchild);
        $update_parent_sql="update menu set arrchild='$parent_menu->arrchild' where menuid=$current_menu->parentid";

        $this->db->query($update_parent_sql);
        $get_parent_cout="select count(*) as countchild from menu where parentid=$current_menu->parentid";
        $childs= $this->db->query($get_parent_cout)->row();
        if(empty($childs) || $childs->countchild==0 )
        {
            $update_parent_arrchilds="update menu set child=0 where menuid=$current_menu->parentid";
            $this->db->query($update_parent_arrchilds);
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    //子集添加
    public function insert_child_menu($menuModel)
    {
        $this->db->trans_begin();
        $this->db->insert('menu',$menuModel);
        $insertId=$this->db->insert_id();//获取添加数据后的主键Id

        $get_parent_sql="select * from menu where menuid=$menuModel->parentid";
        $parent_menu_model=$this->db->query($get_parent_sql)->row();
        $update_parent_sql="update menu set  arrchild='".$parent_menu_model->arrchild.$insertId.",',child=1 where menuid=$menuModel->parentid";
        $this->db->query($update_parent_sql);
        $update_arrchild_sql="update menu set arrchild='$insertId,', arrparentid='".$parent_menu_model->arrparentid."$parent_menu_model->menuid,',path='".$parent_menu_model->path."$insertId,' where menuid=$insertId";
        $this->db->query($update_arrchild_sql);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    public function edit_child_menu($menu_model,$relation)
    {
        $this->db->trans_begin();
        $sql="update menu set title='$menu_model->title',entitle='$menu_model->entitle',menu_control='$menu_model->menu_control',menu_action='$menu_model->menu_action',page_type=$menu_model->page_type where menuid=$menu_model->menuid";
        $this->db->query($sql);
        if(!empty($relation) && $relation>0)
        {
            $sql_relation_child="update menu set menu_control='$menu_model->menu_control',menu_action='$menu_model->menu_action',page_type=$menu_model->page_type where path like '%$menu_model->menuid,%' and arrparentid like '0,%$menu_model->menuid%' and menuid<>$menu_model->menuid";
            $this->db->query($sql_relation_child);
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }
    public function get_all_menu()
    {
        $sql="select * from menu where isuse=1";
        return $this->db->query($sql)->result();
    }

    public function update_menu_icon($menu_model)
    {
        $sql="update menu set icon='$menu_model->icon' where menuid=$menu_model->menuid";
        return $this->db->query($sql);
    }

    public function del_menu_icon($menuid)
    {
        $sql="update menu set icon='' where menuid=$menuid";
        return $this->db->query($sql);
    }
}